package com.ssx.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.ssx.entity.Customer;
import com.ssx.entity.Customervisit;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ssx.entity.Employees;
import com.ssx.entity.Role;
import org.apache.ibatis.annotations.*;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author ssx
 * @since 2020-10-24
 */
public interface CustomervisitMapper extends BaseMapper<Customervisit> {

    @Select("select distinct customervisit.*,customer.cusName,employees.`name` FROM customervisit\n" +
            "LEFT JOIN customer ON customer.cusId=customervisit.cusid\n" +
            "LEFT JOIN employees ON employees.empid=customervisit.empid ${ew.customSqlSegment}")
    @Results({
            @Result(column = "empid",
                    property = "employees",
                    javaType = Employees.class,
                    one = @One(select = "com.ssx.mapper.EmployeesMapper.selectById")
            ),
            @Result(column = "cusId",
                    property = "customer",
                    javaType = Customer.class,
                    one = @One(select = "com.ssx.mapper.CustomerMapper.selectById")
            )
    })
        //员工查询拜访，记录
    List<Customervisit> listFromEmp(@Param(Constants.WRAPPER) Wrapper wrapper);

    @Select("select distinct customervisit.*,customer.cusName,employees.`name` FROM customervisit\n" +
            "LEFT JOIN customer ON customer.cusId=customervisit.cusid\n" +
            "LEFT JOIN employees ON employees.empid=customervisit.empid ${ew.customSqlSegment}")
    @Results({
            @Result(column = "empid",
                    property = "employees",
                    javaType = Employees.class,
                    one = @One(select = "com.ssx.mapper.EmployeesMapper.selectById")
            ),
            @Result(column = "cusId",
                    property = "customer",
                    javaType = Customer.class,
                    one = @One(select = "com.ssx.mapper.CustomerMapper.selectById")
            )
    })
    //经理查询拜访，记录
    List<Customervisit> listFromManger(@Param(Constants.WRAPPER) Wrapper wrapper);

    @Select("select count(*) FROM customervisit\n" +
            "LEFT JOIN customer ON customer.cusId=customervisit.cusid\n" +
            "LEFT JOIN employees ON employees.empid=customervisit.empid ${ew.customSqlSegment}")
    //员工查询拜访，总数
    public int countFromEmp(@Param(Constants.WRAPPER) Wrapper wrapper);

    @Select("select count(*) FROM customervisit\n" +
            "LEFT JOIN customer ON customer.cusId=customervisit.cusid\n" +
            "LEFT JOIN employees ON employees.empid=customervisit.empid ${ew.customSqlSegment}")
    //经理查询拜访，总数
    public int countFromManger(@Param(Constants.WRAPPER) Wrapper wrapper);

    @Insert("insert into customervisit(cusId,empid,content,date)values(#{customer.cusId},#{employees.empid},#{content},#{date})")
    @Override
    int insert(Customervisit entity);

    @Update("update customervisit set cusId=#{customer.cusId},empid=#{employees.empid},content=#{content},date=#{date} where id=#{id}")
    @Override
    int updateById(Customervisit entity);

    @Select("select distinct customervisit.*,customer.cusName,employees.`name` FROM customervisit\n" +
            "LEFT JOIN customer ON customer.cusId=customervisit.cusid\n" +
            "LEFT JOIN employees ON employees.empid=customervisit.empid where id=#{id}")
    @Results({
            @Result(column = "empid",
                    property = "employees",
                    javaType = Employees.class,
                    one = @One(select = "com.ssx.mapper.EmployeesMapper.selectById")
            ),
            @Result(column = "cusId",
                    property = "customer",
                    javaType = Customer.class,
                    one = @One(select = "com.ssx.mapper.CustomerMapper.selectById")
            )
    })
    @Override
    Customervisit selectById(Serializable id);
}
